package com.lanou.projecta.topic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.projecta.R;

import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class TopicFragment extends Fragment{
    private ListView listView;
    private RequestQueue queue;
    private BaseTopic baseTopic;
    private TopicFragmentBaseAdapter adapter;
    private List<BaseTopic.DataEntity.ExpertListEntity> baseTopic1;
    private String url = "http://c.3g.163.com/newstopic/list/expert/0-10.html";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_topic,null);
        listView = (ListView) view.findViewById(R.id.topic_listview);
        queue = Volley.newRequestQueue(getActivity());

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                baseTopic = gson.fromJson(response,BaseTopic.class);
                baseTopic1 = baseTopic.getData().getExpertList();
                adapter = new TopicFragmentBaseAdapter(baseTopic1,getActivity());
                listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

     queue.add(request);

        return view;
    }
}
